<template>
  <div class="login-container">
    <!-- 登录 -->
    <div class="login-wrap">
      <div class="login">
        <div class="loginform">
          <div class="content">
            <el-form
              ref="ruleFormRef"
              :model="userInfo"
              :rules="loginRules"
              status-icon
            >
              <el-form-item prop="phone">
                <div class="input-text clearFix">
                  <span></span>
                  <el-input
                    type="text"
                    class="input"
                    placeholder="邮箱/用户名/手机号"
                    name="手机"
                    v-model="userInfo.phone"
                    autocomplete
                  />
                </div>
              </el-form-item>
              <el-form-item style="margin: 0" prop="password">
                <div class="input-text clearFix">
                  <span class="pwd"></span>
                  <el-input
                    class="input"
                    type="text"
                    placeholder="请输入密码"
                    v-model="userInfo.password"
                    name="密码"
                    autocomplete
                  /></div
              ></el-form-item>
              <button class="btn" @click.prevent="loginHandler(ruleFormRef)">
                登&nbsp;&nbsp;录
              </button>
            </el-form>
            <div class="call clearFix">
              <router-link class="register" to="/register"
                >立即注册</router-link
              >
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 底部 -->
    <div class="copyright">
      <ul>
        <li>关于我们</li>
        <li>联系我们</li>
        <li>联系客服</li>
        <li>商家入驻</li>
        <li>营销中心</li>
        <li>手机尚品汇</li>
        <li>销售联盟</li>
        <li>尚品汇社区</li>
      </ul>
      <div class="address">地址：北京市昌平区宏福科技园综合楼6层</div>
      <div class="beian">京ICP备19006430号</div>
    </div>
  </div>
</template>
<script>
import { defineComponent } from "vue";
export default defineComponent({
  name: "Login",
});
</script>
<script setup>
import { ref, reactive } from "vue";
import { useRoute, useRouter } from "vue-router";
import { phoneReg, pwdReg } from "@/utils/reg";
import { reqLogin } from "@/api/login";
import { setToken } from "@/utils/auth";
import { useUserInfoStore } from "@/stores/user";
import { ElMessage } from "element-plus";

// data
const userInfo = reactive({
  phone: "",
  password: "",
});

// route
const router = useRouter();

// store
const userInfoStore = useUserInfoStore();

// ref
const ruleFormRef = ref();

// 校验规则
const validateUsername = (rule, value, callback) => {
  if (value.length < 11) {
    callback(new Error("手机号码长度不能小于11位"));
  } else {
    if (!phoneReg.test(value)) {
      callback(new Error("手机号码格式不正确"));
    } else {
      callback();
    }
  }
};
const validatePassword = (rule, value, callback) => {
  if (value.length < 6) {
    callback(new Error("密码长度不能小于6位"));
  } else {
    callback();
  }
};
const loginRules = {
  phone: [{ required: true, validator: validateUsername }],
  password: [{ required: true, trigger: "blur", validator: validatePassword }],
};
const route = useRoute();
// 方法
// 1.点击登陆按钮去登陆
const loginHandler = async (formEl) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      const { phone, password } = userInfo;
      const params = { phone, password };
      const result = await reqLogin(params);
      const { redirect } = route.query;
      if (result.code == 200) {
        // 存储token
        setToken(result.data.token);
        userInfoStore.getUserInfo();
        router.push(redirect || "/home");
        ElMessage.success("登陆成功");
      } else {
        ElMessage.error(result.message);
      }
    } else {
      ElMessage.error("格式错误!");
    }
  });
};
</script>

<style lang="less" scoped>
.login-container {
  .login-wrap {
    height: 487px;
    background-color: #e93854;

    .login {
      width: 1200px;
      height: 487px;
      margin: 0 auto;
      background: url(./images/loginbg.png) no-repeat;
    }

    .loginform {
      width: 420px;
      height: 306px;
      box-sizing: border-box;
      background: #fff;
      float: right;
      top: 45px;
      position: relative;
      padding: 20px;

      .tab {
        li {
          width: 50%;
          float: left;
          text-align: center;

          a {
            width: 100%;
            display: block;
            height: 50px;
            line-height: 50px;
            font-size: 20px;
            font-weight: 700;
            color: #333;
            border: 1px solid #ddd;
            box-sizing: border-box;
            text-decoration: none;
          }

          .current {
            border-bottom: none;
            border-top-color: #28a3ef;
            color: #e1251b;
          }
        }
      }

      .content {
        width: 380px;
        height: 316px;
        box-sizing: border-box;
        border-top: none;
        padding: 18px;

        form {
          margin: 15px 0 18px 0;
          font-size: 12px;
          line-height: 18px;

          .input-text {
            margin-bottom: 16px;

            span {
              float: left;
              width: 37px;
              height: 32px;
              border: 1px solid #ccc;
              background: url(./images/icons.png) no-repeat -10px -201px;
              box-sizing: border-box;
              border-radius: 2px 0 0 2px;
            }
            i {
              color: #e93854;
            }

            .pwd {
              background-position: -72px -201px;
            }

            .input {
              width: 302px;
              height: 32px;
              box-sizing: border-box;
              border-left: none;
              float: left;
              font-size: 14px;
              line-height: 22px;
              outline: none;
            }
          }

          .setting {
            label {
              float: left;
            }

            .forget {
              float: right;
            }
          }

          .btn {
            background-color: #e1251b;
            padding: 6px;
            border-radius: 0;
            font-size: 16px;
            font-family: 微软雅黑;
            word-spacing: 4px;
            border: 1px solid #e1251b;
            color: #fff;
            width: 100%;
            height: 36px;
            margin-top: 25px;
            outline: none;
          }
        }

        .call {
          margin-top: 30px;

          ul {
            float: left;

            li {
              float: left;
              margin-right: 5px;
            }
          }

          .register {
            float: right;
            font-size: 15px;
            line-height: 38px;
          }

          .register:hover {
            color: #4cb9fc;
            text-decoration: underline;
          }
        }
      }
    }
  }
  .copyright {
    width: 1200px;
    margin: 0 auto;
    text-align: center;
    line-height: 24px;

    ul {
      li {
        display: inline-block;
        border-right: 1px solid #e4e4e4;
        padding: 0 20px;
        margin: 15px 0;
      }
    }
  }
  :deep(.el-form-item__error) {
    top: 70%;
    left: 10%;
  }
}
</style>
